#功    能：完整小说录入小说数据库
#功能函数： readtxtcontent()

# 使用方法：把小说txt放在该目录：static/keywords/xiaoshuo.txt，
        #读取小说的时候把这个函数放到view中直接调用就可以，用完删掉，直接存入小说数据库

import os
import random
from newsblog import models
from web.settings import BASE_DIR

def readtxtcontent():
    with open(os.path.join(BASE_DIR, 'static\\keywords\\../../static/keywords/xiaoshuo.txt'), encoding="utf-8") as file_to_read:
        content = file_to_read.read()  # 结果为str类型
        print(type(content))
        print(len(content))
        a = 1
        for i in range(10000):
            abc = content[a:a+300]
            a=a+300
            print("--------------------------------")
            print(abc)
            if abc !='':
                models.Xiaoshuo.objects.create(content=abc)
            else:#导入数据库
                print(("读取结束一共有%d个段落")% i)
                exit()

def readxiaoshuofromsql():
    n = 1
    i = random.randint(0, models.Xiaoshuo.objects.count() - n)
    myuser = list(models.Xiaoshuo.objects.all()[i:i + n])
    return myuser[0]
